﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GioHangViet.Models;
using GioHangViet.Areas.Admin.Models;

namespace GioHangViet.Areas.Admin.Controllers
{ 
    public class ComterController : Controller
    {
        private readonly GoLungTungModel _db = new GoLungTungModel();
        private readonly ProcessAlias _pa = new ProcessAlias();

        //
        // GET: /Admin/Comter/

        public ViewResult Index(int id)
        {
            if (id < 1)
            {
                id = 1;
            }
            ViewBag.CurrentPage = id;
            ViewBag.countRecord = _db.Comters.Count();
            ViewBag.pageSize = Convert.ToDouble(Resources.Constant.PageSize);
            var model = _db.Comters.ToList().Skip((id - 1) * Convert.ToInt32(Resources.Constant.PageSize)).Take( Convert.ToInt32(Resources.Constant.PageSize));
            return View(model);
        }

        //
        // GET: /Admin/Comter/Create

        public ActionResult Create()
        {
            var comter = new Comter { Alias = "New" };
            return View(comter);
        } 

        //
        // POST: /Admin/Comter/Create

        [HttpPost]
        public ActionResult Create(Comter comter)
        {
            if (ModelState.IsValid)
            {
                var name = comter.Name;
                var alias = _pa.CreateAlias(name, "Comters", "Name");
                comter = new Comter { Alias = alias, Name = name };
                _db.Comters.Add(comter);
                _db.SaveChanges();
                return RedirectToAction("Index", new{ id = 1});  
            }

            return View(comter);
        }
        
        //
        // GET: /Admin/Comter/Edit/5
 
        public ActionResult Edit(string id)
        {
            Comter comter = _db.Comters.Find(id);
            return View(comter);
        }

        //
        // POST: /Admin/Comter/Edit/5

        [HttpPost]
        public ActionResult Edit(Comter comter)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(comter).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", new{ id = 1});
            }
            return View(comter);
        }

        //
        // GET: /Admin/Comter/Delete/5
 
        public ActionResult Delete(string id)
        {
            var comter = _db.Comters.Find(id);
            if (comter != null)
            {
                if (_db.ComterProducts.Any(product => product.Alias == id))
                {
                    ViewBag.ErrorMessage = Resources.Message.DeleteContrainComter;
                    ViewBag.CurrentPage = 1;
                    ViewBag.countRecord = _db.Comters == null ? 0 : _db.Comters.Count();
                    ViewBag.pageSize = Convert.ToDouble(Resources.Constant.PageSize);
                    var model =
                        _db.Comters.ToList().Take(Convert.ToInt32(Resources.Constant.PageSize));
                    return View("Index", model);
                }
                _db.Comters.Remove(comter);
                _db.SaveChanges();
                return RedirectToAction("Index", new {id = 1});
            }
            return View("Error");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}